<template>
  <div class="user">
    <div class="search">
      <page-search
        :IFormConfig="IFormConfig"
        @handleResetClick="handleResetClick"
        @handleQueryClick="handleQueryClick"
      />
    </div>
    <div class="content">
      <page-content
        ref="pageContentRef"
        :ContentTableConfig="ContentTableConfig"
        pageName="users"
        @newBtnClick="handleNewData"
        @editBtnClick="handleEditData"
      >
        <template #enable="scope">
          <el-tag :type="scope.row.enable ? 'success' : 'danger'" size="medium">
            {{ scope.row.enable ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </page-content>
    </div>
    <page-model
      ref="pageModelRef"
      pageName="users"
      :defaultInfo="defaultInfo"
      :modelConfig="modelConfigRef"
    ></page-model>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'

import PageSearch from '@/components/page-search/index'
import PageContent from '@/components/page-content/index'
import PageModel from '@/components/page-model/index'
import { IFormConfig } from './config/search.config' // 搜索栏配置
import { ContentTableConfig } from './config/content.cofig' // 表格配置
import { modelConfig } from './config/model.config'
import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModel } from '@/hooks/usePageModel'
import { useStore } from '@/store'
export default defineComponent({
  name: 'users',
  components: { PageSearch, PageContent, PageModel },
  setup() {
    // 获取组件实例调用方法
    const [pageContentRef, handleResetClick, handleQueryClick] = usePageSearch()
    // 新增时执行此方法显示password输入框
    const addCallback = () => {
      const passwordItem = modelConfig.formItems.find((item: any) => item.filed === 'password')
      passwordItem.isHidden = false
    }
    // 编辑时执行此方法隐藏password输入框
    const editCallback = () => {
      const passwordItem = modelConfig.formItems.find((item: any) => item.filed === 'password')
      passwordItem.isHidden = true
    }
    const [pageModelRef, defaultInfo, handleNewData, handleEditData] = usePageModel(addCallback, editCallback)
    // 动态添加表单中的部门和角色options数据
    // 使用computed包裹 解决刷新数据丢失问题
    const store = useStore()
    const modelConfigRef = computed(() => {
      const departmentItem = modelConfig.formItems.find((item) => item.filed === 'departmentId')
      departmentItem.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })
      const roleItem = modelConfig.formItems.find((item) => item.filed === 'roleId')
      roleItem.options = store.state.entireRole.map((item) => {
        return { title: item.name, value: item.id }
      })
      return modelConfig
    })
    return {
      IFormConfig, // 搜索栏配置
      ContentTableConfig, // 表格配置
      modelConfigRef, // 弹窗配置
      pageContentRef,
      handleResetClick,
      handleQueryClick,
      pageModelRef,
      defaultInfo,
      handleNewData,
      handleEditData
    }
  }
})
</script>

<style scoped lang="less">
.search {
  background-color: #fff;
  border-radius: 5px;
  margin-bottom: 20px;
}
.content {
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
</style>
